<template>
  <div>
    <g-dynamic-table
      type="editions"
      ref="table"
      :fetch="fetchDataList"
      :isPage="false"
      :showExport="true"
      @action-click="handleActionClick"
    >
    </g-dynamic-table>
  </div>
</template>

<script lang="ts">
  import { EditionListDto, EditionServiceProxy } from '/@/shared/service-proxies';
  import CreateOrEditEdition from './create-or-edit-edition/create-or-edit-edition.vue';
  import { defineComponent } from 'vue';
  import GDynamicTable from '/@/shared/components/g-dynamic-table/index.vue';
  import { EVENT_BTN_ENUM } from '/@/shared/components/g-dynamic-table/enums/event-btn-enum';
  import { AppComponentBase } from '/@/shared';
  export default defineComponent({
    name: 'editions-news',
    components: {
      GDynamicTable,
      CreateOrEditEdition,
    },
    mixins: [AppComponentBase],
    data() {
      return {
        editionService: new EditionServiceProxy(),
        dataList: [] as EditionListDto[],
        selectItems: [] as any[],
      };
    },

    methods: {
      /**
       * 按钮回调事件
       */
      handleActionClick(event: string, item?: any) {
        switch (event) {
          case EVENT_BTN_ENUM.CREATE:
            this.create();
            break;
          case EVENT_BTN_ENUM.DELETE:
            this.deleteItem(item);
            break;
          case EVENT_BTN_ENUM.BULK_EDIT:
            this.bulkEdit();
            break;
          case EVENT_BTN_ENUM.EDIT:
            this.edit(item);
            break;
          default:
            break;
        }
      },
      //获取数据
      fetchDataList(otherQuery) {
        const { EditionName } = (this.$refs.table as any).getSearchProps();
        return new Promise((resolve, reject) => {
          this.editionService
            .getEditionsByFile(EditionName, null, 20, 0)
            .finally(() => {
              // finishedCallback!();
            })
            .then((res) => {
              resolve(res.items);
              // this.showPaging(res);
            })
            .catch((error) => {
              reject(error);
            });
        });
      },
      /**
       * 批量编辑
       */
      bulkEdit() {
        this.selectItems = [];
        let arr = (this.$refs.table as any).getSelectRowKeys();
        if (arr.length < 1) {
          return this.notify.warn(this.l('PleaseSelectAtLeastOneItem'));
        }
        this.selectItems = arr;
        this.modalHelper
          .create(CreateOrEditEdition, { pageDataList: this.selectItems })
          .subscribe((res) => {
            if (res) {
              (this.$refs.table as any).reloadGoFirstPage();
            }
          });
      },
      create(item?: EditionListDto) {
        this.selectItems = [];
        // this.item = !!item ? item : ({} as UserListDto);
        if (!!item && item.id) {
          this.selectItems = [item.id];
        }

        this.modalHelper
          .create(CreateOrEditEdition, { pageDataList: this.selectItems })
          .subscribe((res) => {
            if (res) {
              (this.$refs.table as any).reloadGoFirstPage();
            }
          });
      },
      edit(item?: EditionListDto) {
        this.selectItems = [];
        // this.item = !!item ? item : ({} as UserListDto);
        if (!!item && item.id) {
          this.selectItems = [item.id];
        }
        this.modalHelper
          .create(CreateOrEditEdition, { pageDataList: this.selectItems })
          .subscribe((res) => {
            if (res) {
              (this.$refs.table as any).reloadGoFirstPage();
            }
          });
      },
      /**
       * 单个删除
       */
      deleteItem(item: EditionListDto) {
        if ((item as any).isStatic) {
          this.notify.warn(
            this.l('XUserCannotBeDeleted', AppConsts.userManagement.defaultAdminUserName),
          );
          return;
        }
        (this.$refs.table as any).setLoading(true);
        this.editionService
          .deleteEdition(item.id)
          .finally(() => {
            (this.$refs.table as any).setLoading(false);
          })
          .then(() => {
            (this.$refs.table as any).reloadGoFirstPage();
            this.notify.success(this.l('SuccessfullyDeleted'));
          });
      },
    },
  });
</script>
